package com.wwj.auction.login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.login.entity.SellerAudit;
import com.wwj.auction.login.entity.SellerAuditImage;
import com.wwj.auction.login.entity.vo.SellerAuditImageVo;
import com.wwj.auction.login.entity.vo.SellerAuditVo;
import com.wwj.auction.login.enums.AuditStatus;
import com.wwj.auction.login.enums.SellerAuditStatus;
import com.wwj.auction.login.mapper.SellerAuditImageMapper;
import com.wwj.auction.login.mapper.SellerAuditMapper;
import com.wwj.auction.login.mapper.UserInfoMapper;
import com.wwj.auction.login.service.SellerAuditService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-26
 */
@Service
public class SellerAuditServiceImpl extends ServiceImpl<SellerAuditMapper, SellerAudit> implements SellerAuditService {

    @Resource
    private SellerAuditImageMapper sellerAuditImageMapper;
    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(SellerAuditVo sellerAuditVo, Integer userId) {
        // 查询是否有正在审核流程中的内容
        SellerAudit sAudit = baseMapper.selectOne(
                Wrappers.lambdaQuery(SellerAudit.class)
                        .eq(SellerAudit::getUserId, userId)
                        .eq(SellerAudit::getStatus, AuditStatus.AUDIT_ING.getStatus()));
        if (sAudit == null) {
            // 若当前没有正在审核流程中的内容，则保存这一次的审核流程
            // 封装审核内容
            SellerAudit sellerAudit = new SellerAudit();
            BeanUtils.copyProperties(sellerAuditVo, sellerAudit);
            // 处理店铺名
            sellerAudit.setStoreName(sellerAuditVo.getName());
            // 设置审核流程为审核中
            sellerAudit.setStatus(SellerAuditStatus.SELLER_AUDIT_ING.getStatus());
            // 设置用户id
            sellerAudit.setUserId(userId);
            // 设置审核时间
            sellerAudit.setCreateTime(LocalDateTime.now());
            // 保存
            baseMapper.insert(sellerAudit);
            // 处理审核材料中的图片信息
            List<SellerAuditImageVo> sellerAttachList = sellerAuditVo.getSellerAttachList();
            sellerAttachList.forEach(sellerAuditImageVo -> {
                SellerAuditImage sellerAuditImage = new SellerAuditImage();
                // 属性拷贝
                BeanUtils.copyProperties(sellerAuditImageVo, sellerAuditImage);
                // 设置外键，标识这些审核图片来源于哪条审核记录
                sellerAuditImage.setSellerAuditId(sellerAudit.getId());
                // 保存
                sellerAuditImageMapper.insert(sellerAuditImage);
            });
        } else {
            // 若是当前有正在审核流程中的内容，则修改该流程的状态为审核中
            sAudit.setStatus(SellerAuditStatus.SELLER_AUDIT_ING.getStatus());
            // 更新
            baseMapper.updateById(sAudit);
        }
    }

    @Override
    public Integer getStatus(Integer userId) {
        LambdaQueryWrapper<SellerAudit> wrapper = Wrappers.lambdaQuery(SellerAudit.class);
        List<SellerAudit> sellerAuditList = baseMapper.selectList(
                wrapper.eq(SellerAudit::getUserId, userId)
                        .orderByDesc(SellerAudit::getId));
        if (sellerAuditList == null || sellerAuditList.size() <= 0) {
            // 若为空，则说明当前用户从未进行过申请成为卖家
            return 0;
        }
        // 返回最新数据
        return sellerAuditList.get(0).getStatus();
    }
}
